/*
 * @Author: sdq 483657864@qq.com
 * @Date: 2025-04-17 14:19:16
 * @LastEditors: sdq 483657864@qq.com
 * @LastEditTime: 2025-04-19 09:33:42
 * @FilePath: \ranyu_anchor_new\src\IM\listenter\imConnectListener.ts
 * @Description: 连接状态监听
 */
import { EaseChatClient } from "../initImSdk";
import { useImStatusStore } from "@/store";
const imStore = useImStatusStore();

export const imConnectListener = () => {
  /**
   * @description mountConnectEventListener - 监听连接状态
   */
  const mountConnectEventListener = () => {
    EaseChatClient.addEventHandler("connection", {
      onConnected: () => {
        // 连接成功
        ElMessage.success("IM连接成功");
        imStore.CHANGE_LOGIN_STATUS(true);
      },
      onDisconnected: () => {
        // 断开连接
        ElMessage.success("断开连接成功");
        imStore.CHANGE_LOGIN_STATUS(false);
      },
      onOnline: () => {
        ElMessage.error("已连接本地网络");
        imStore.CHANGE_NETWORK_STATUS(true);
      },
      onOffline: () => {
        ElMessage.error("本机网络掉线");
        imStore.CHANGE_NETWORK_STATUS(false);
      },
    });
  };
  /**
   * @description getMyUserInfos - 获取登陆用户属性
   */
  const getMyUserInfos = () => {
    const userId = EaseChatClient.user;
    console.log("当前我是" + userId);
    imStore.getMyUserInfo(userId);
  };
  return {
    mountConnectEventListener,
    getMyUserInfos,
  };
};
